package com.boot_reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boot_reggie.Service.impl.SendMailServiceimpl;
import com.boot_reggie.Service.impl.UserServiceImpl;
import com.boot_reggie.common.Result;
import com.boot_reggie.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/code")
@Slf4j
public class CodeController {
    @Autowired
    private SendMailServiceimpl sendMailServiceimpl;
    @Autowired
    private UserServiceImpl userService;
    @GetMapping
    public Result<String> code(String name,HttpSession session){
        System.out.println(name);
        String title = "瑞吉外卖注册验证码";
        return Result.success(sendMailServiceimpl.Context(name,title,session));
    }

    @GetMapping("login")
    public Result<String> loginCode(String phone,HttpSession session){
        String title = "瑞吉外卖登录验证码";
        LambdaQueryWrapper<User> lwq = new LambdaQueryWrapper<>();
        lwq.eq(User::getPhone,phone);
        User user = userService.getOne(lwq);
        if(user != null){
//            发送验证码
            return Result.success(sendMailServiceimpl.Context(user.getMailbox(),title,session));
        }else {
//            不存在
            return Result.error("手机号不存在");
        }
    }
}
